<?php
include_once dirname(__FILE__).'/RsyncBase.class.php';
include_once dirname(dirname(__FILE__)).'/__queue_global.php';
include_once SERVER_ROOT_PATH.'/include/config/ml_search_config.php';


class mlSearchContentRsync extends RsyncBase
{
    
    var $isRsyncSuccess;

    /**
     * 新建目录及目录检查
     */
    public function mkReadyDir()
    {
        $this->mkDir(ML_SEARCH_TMP_CONTENT_DIR);
        $this->mkDir(ML_SEARCH_CONTENT_RSYNC_PATH_ERROR_BAK);
        $this->mkDir(ML_SEARCH_CONTENT_RSYNC_PATH_SUCCESS_BAK);
        $this->processnum = 10;
    }
    /**
     *处理入口
     */
    public function do_work()
    {
        //得到准备推送的文件
        $readyFileArr = $this->getDirFile(ML_SEARCH_TMP_CONTENT_DIR,'/meila_gd_(.*)\.txt/is');
        //遍历
        foreach ($readyFileArr as $file)
        {
            //检查是否被锁定
            if($this->isFileWriteLock($file))
            {
                continue;
            }
            
            if($this->rsyncFileNopwd($file,ML_SEARCH_CONTENT_RSYNC_IP,ML_SEARCH_CONTENT_RSYNC_MODULE))
            {
                echo "\n $file \n";
                $isRsyncSuccess[$file] = true;
            }
            //备份数据
            if(true==$isRsyncSuccess[$file])
            {
                $sucessFile = ML_SEARCH_CONTENT_RSYNC_PATH_SUCCESS_BAK.basename($file);
                $this->mvFileToBak($file,$sucessFile);
                continue;
            }

            //备份数据
            if(false==$isRsyncSuccess[$file])
            {
                $errorFile = ML_SEARCH_CONTENT_RSYNC_PATH_ERROR_BAK.basename($file);
                $this->cpFileToBak($file,$errorFile);
                continue;
            }
        }
    }
}

ml_run_queue_check(ML_CRONPHP_SEARCH_CONTENT);
$obj = new mlSearchContentRsync;
